﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CommonData;

namespace DataCentral.BusinessDataObjects
{
    public class UserBO
    {
 		protected Guid _userID;
		protected string _userName = String.Empty;
		protected decimal _roleID;
		protected string _password = String.Empty;
		protected string _description = String.Empty;
		protected DateTime _createdDate;
		protected Guid _createdUserID = Guid.Empty;
		protected DateTime _lastUpdatedDate;
		protected Guid _lastUpdatedUserID = Guid.Empty;
		
		public UserBO()
		{
		}

        public UserBO(SqlDataReader reader)
		{
			this.LoadFromReader(reader);
		}

        protected void LoadFromReader(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed && reader.Read())
            {
                _userID = reader.GetGuid(0);
                if (!reader.IsDBNull(1)) _userName = reader.GetString(1);
                if (!reader.IsDBNull(2)) _roleID = reader.GetDecimal(2);
                if (!reader.IsDBNull(3)) _password = CommonFunctions.DecodeFrom64(reader.GetString(3));
                if (!reader.IsDBNull(4)) _description = reader.GetString(4);
                if (!reader.IsDBNull(5)) _createdDate = reader.GetDateTime(5);
                if (!reader.IsDBNull(6)) _createdUserID = reader.GetGuid(6);
                if (!reader.IsDBNull(7)) _lastUpdatedDate = reader.GetDateTime(7);
                if (!reader.IsDBNull(8)) _lastUpdatedUserID = reader.GetGuid(8);
            }
        }
		
		#region Public Properties
		public Guid UserID
		{
			get {return _userID;}
		}
		
		public string UserName
		{
			get {return _userName;}
			set {_userName = value;}
		}

		public decimal RoleID
		{
			get {return _roleID;}
			set {_roleID = value;}
		}

		public string Password
		{
			get {return _password;}
			set {_password = value;}
		}

		public string Description
		{
			get {return _description;}
			set {_description = value;}
		}

		public DateTime CreatedDate
		{
			get {return _createdDate;}
			set {_createdDate = value;}
		}

		public Guid CreatedUserID
		{
			get {return _createdUserID;}
			set {_createdUserID = value;}
		}

		public DateTime LastUpdatedDate
		{
			get {return _lastUpdatedDate;}
			set {_lastUpdatedDate = value;}
		}

		public Guid LastUpdatedUserID
		{
			get {return _lastUpdatedUserID;}
			set {_lastUpdatedUserID = value;}
		}
		#endregion

        public UserBO GetUserBOByUsename(string userName, string password)
        {
            List<ParameterObject> lstParams = new List<ParameterObject>();
            lstParams.Add(new ParameterObject("@UserName", userName));
            lstParams.Add(new ParameterObject("@Password", password));
            SqlDataReader sqlReader = null;
            CommonDataAccess dataAccess = new CommonDataAccess();
            sqlReader = dataAccess.GetDataReader(sqlReader, "FBB_SelectUser_ByPassName", lstParams);
            if (sqlReader != null && sqlReader.HasRows)
                return new UserBO(sqlReader);
            else 
                return null;
        }

        public UserBO GetUserBOByUserID(Guid userID)
        {
            List<ParameterObject> lstParams = new List<ParameterObject>();
            lstParams.Add(new ParameterObject("@UserID", userID));
            SqlDataReader sqlReader = null;

            CommonDataAccess dataAccess = new CommonDataAccess();
            sqlReader = dataAccess.GetDataReader(sqlReader, "FBB_SelectUser", lstParams);
            if (sqlReader != null && sqlReader.HasRows)
                return new UserBO(sqlReader);
            else
                return null;
        }

        public string GetUserRoleName(decimal roleID)
        {
            string strResult = string.Empty;
            List<ParameterObject> lstParams = new List<ParameterObject>();
            lstParams.Add(new ParameterObject("@RoleID", roleID));
            SqlDataReader sqlReader = null;
            CommonDataAccess dataAccess = new CommonDataAccess();
            sqlReader = dataAccess.GetDataReader(sqlReader, "FBB_SelectRole", lstParams);
            if (sqlReader != null && sqlReader.Read())
                strResult = sqlReader["RoleName"].ToString();
            return strResult;
        }

        public void UpdateUserBO(UserBO userBO)
        {
            List<ParameterObject> lstParams = new List<ParameterObject>();
            lstParams.Add(new ParameterObject("@UserID", userBO.UserID));
            lstParams.Add(new ParameterObject("@UserName", userBO.UserName));
            lstParams.Add(new ParameterObject("@Password", CommonFunctions.EncodeTo64(userBO.Password)));
            lstParams.Add(new ParameterObject("@Description", userBO.Description));
            lstParams.Add(new ParameterObject("@RoleID", userBO.RoleID));
            lstParams.Add(new ParameterObject("@CreatedDate", userBO.CreatedDate));
            lstParams.Add(new ParameterObject("@CreatedUserID", userBO.CreatedUserID));
            lstParams.Add(new ParameterObject("@LastUpdatedDate", userBO.LastUpdatedDate));
            lstParams.Add(new ParameterObject("@LastUpdatedUserID", userBO.LastUpdatedUserID));
            CommonDataAccess dataAccess = new CommonDataAccess();
            dataAccess.ExecuteSprocNonQuery("FBB_InsertUpdateUser", lstParams);
        }

        public DataTable GetAccessRightList(decimal userRoleID)
        {
            List<ParameterObject> lstParams = new List<ParameterObject>();
            lstParams.Add(new ParameterObject("@RoleID", userRoleID));
            CommonDataAccess dataAccess = new CommonDataAccess();
            return dataAccess.GetDataTableBySproc("FBB_SelectAccessRightsByRoleID", lstParams);
        }
    }
}
